package d1215;

import java.util.Scanner;

public class StudentSystem {
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {

        StringBuilder s = new StringBuilder();
        while (true) {
            {
                System.out.println("欢迎来到学生管理系统");
                System.out.println("请选择操作：");
                System.out.println("1.添加学生信息\t2.修改学生信息\t3.查询学生信息\t4.删除学生信息\t0.退出");
                String z = scanner.next();
                switch (z) {
                    case "1":
                        add(s);
                        break;
                    case "2":
                        change(s);
                        break;
                    case "3":
                        find( s);
                        break;
                    case "4":
                        delete( s);
                        break;
                    case "0":
                        System.out.println("退出程序");
                        return;
                    default:
                        System.out.println("输入错误请重新输入");
                }
            }
        }
    }

    private static void delete(StringBuilder s) {
        System.out.println("    删除学生信息    ");
        System.out.println("请输入要删除的学生学号");
        String  id = scanner.next();
        int index = s.indexOf(" "+id+" ");
        if (index==-1){
            System.out.println("不存在此学号");
           return;
        }
        int end = s.indexOf(";",index);
        s.delete(index,end);

    }

    private static void find(StringBuilder s) {
        System.out.println("     查询学生信息      ");
        String[] split = s.toString().split(";");
        System.out.println("学号\t姓名\t年龄");
        for (int i = 0; i <split.length ; i++) {
            String s1 = split[i];
            String[] split1 = s1.split(",");
            System.out.println(split1[0]+" "+split1[1]+" "+split1[2]);
        }
    }

    private static void change(StringBuilder s ) {
        System.out.println("     修改学生信息     ");
        System.out.println("请输入要修改的学生学号");
        String  id = scanner.next();
        int index = s.indexOf(" "+id+" ");
        if (index==-1){
            System.out.println("不存在此学号");
          return;
        }
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        System.out.println("请输入学生年龄");
        String age = scanner.next();
        String s1 = id +","+name+","+age;
        int end = s.indexOf(";",index);
        s.replace(index,end,s1);
    }

    private static void add(StringBuilder s) {
        System.out.println("      添加学生信息    ");
        System.out.println("请输入学生学号");
        String id =scanner.next();
        //判断学号是否存在

        int index = s.indexOf(" "+id+" ");
        if (index!=-1){
            System.out.println("学号已存在");
          return;
        }
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        System.out.println("请输入学生年龄");
        String age = scanner.next();
        String  ss = " "+id+" ,"+name+","+age+";";
        s.append(ss);

        System.out.println("添加成功");

    }
}